// pages/index/goods-detail/goods-detail.js
var utils = require('../../../utils/util.js')
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: {},
    tickets: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var goodsId = options.id;
    var goodsurl = app.globalData.ydnBase + '/index.php/Weshop/SApi/index/key/2sdfdd332342341dddff46e189670/m/getPageGoodsInfo/wid/39/goods_id/' + goodsId + '/sign/' + app.globalData.sign;

    utils.http(goodsurl, this.processGoodsData)

  },
  processGoodsData: function (data) {
    var data = data.datas;
    var goods = data.goods;
    var ticket = data.goods_ticket;
    var cards = data.ma_bussines_card;
    var tickets = [];
    console.log(data)
    if (!data) {
      return;
    }
    if (ticket) {
      data.goods_ticket.forEach(function (item, index) {
        var temp = {
          index: index,
          id: item.id,
          name: item.goods_name,
          price: item.goods_price,
          selected: false
        }
        tickets.push(temp)
      })
    }
    if (cards) {
      var card = {
        beginTime: cards.c_begintime,
        endTime: cards.c_endtime,
        cdesc: cards.c_desc
      }
    }
    var goods = {
      goodsId: data.goods_id,
      price: data.goods_price,
      imgUrls: data.pic_swiper,
      orderNum: data.order_goods_sum.goods_num,
      name: goods.goods_name,
      storeCount: goods.store_count,
      viewCount: goods.view_count,
      showViewCount: goods.show_view_count,
      details: goods.goods_content,
    }
    this.setData({
      goods: goods,
      ticket:ticket,
      tickets: tickets,
      card: card
    })
  },
  selectTicket: function (e) {
    var id = e.currentTarget.id,
      list = this.data.tickets,
      name = e.currentTarget.dataset.name,
      price = e.currentTarget.dataset.price,
      selectedArry = [];
    this.setData({
      "ticket.id": id,
      "goods.name": name,
      "goods.price": price
    })
    for (var i = 0, len = list.length; i < len; ++i) {
      if (list[i].id == id) {
        list[i].selected = true;
      } else {
        list[i].selected = false
      }
      selectedArry.push(list[i].selected)
    }
    var isAllSelected = selectedArry.every(function (item) {
      return item == false
    })
    if (isAllSelected) {
      console.log(1)
    }
    this.setData({
      tickets: list
    })
  },
  selectGroup: function (e) {
    var id = e.currentTarget.id, list = this.data.groupsList;
    for (var i = 0, len = list.length; i < len; ++i) {
      if (list[i].id == id) {
        list[i].selected = !list[i].selected
      } else {
        list[i].selected = false
      }
    }
    this.setData({
      groupsList: list
    })
  },
  tapBuy: function (e) {
    var that=this;
    var id = e.currentTarget.dataset.id,
      goodsId = e.currentTarget.dataset.goodsId;
      if (this.data.ticket){
        if (!id) {
          wx.showToast({
            title: '请选择门票类别',
          })
          return;
        }
        console.log(goodsId)
        wx.navigateTo({
          url: '../tickDetail/tickDetail?goodsId=' + goodsId + '&id=' + id,
        })
      }else{
        that.tapCart(e);
        wx.switchTab({
          url: '../../shoppingCar/shoppingCar?goodsId=' + goodsId ,
        })
      }
  },
  tapCart: function (e) {
    var goodsId = e.currentTarget.dataset.goodsId;
    var url = app.globalData.ydnBase + "/index.php/Weshop/SApi/index/m/addPageCart/key/2sdfdd332342341dddff46e189670/wid/39";
    wx.request({
      url: url,
      data: {
        sign: app.globalData.sign,
        ctype: 1,
        goods_id: goodsId,
        goods_num: 1,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        wx.showToast({
          title: res.data.msg,
        })
      },
      fail: function(res){
        wx.showToast({
          title: res.data.msg,
        })
      }
    })
  }
  ,

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})